package com.mrd.gtimp.business.service;

import com.mrd.base.BaseBiz;
import com.mrd.exception.QueryException;
import com.mrd.gtimp.business.entity.CompanyQp;
import com.mrd.gtimp.business.repository.CompanyQpDao;
import com.mrd.spring.repository.BaseRepository;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

import javax.annotation.Resource;


/**
 * <p> application name: gtimp <p> copyright: Copyright@2012 明睿达科技有限公司
 */

/**
 * @author yangyl
 */
@Service
public class CompanyQpBiz extends BaseBiz<CompanyQp> {


    @Resource
    private CompanyQpDao companyQpDao;

    @Resource
    // 该处指定注入BaseRepository的dao对象
    @Qualifier("companyQpDao")
    public void setBaseRepository(BaseRepository<CompanyQp, String> baseRepository) {
        super.baseRepository = baseRepository;
    }


    //生产销售单位 sql
    public String find(String keyWord) {
        String sql = "select t.company_code,t.company_name, t.company_address, t.company_phone, t.company_fax,t.company_co_unite,t.status from v_cdams_qp_company t where 1=1 ";
        if (StringUtils.isNotEmpty(keyWord)) {
            sql += " and (COMPANY_CODE like '%" + keyWord.trim() + "%' or COMPANY_NAME like '%"
                    + keyWord.trim() + "%')";
        }
        return sql;
    }


    /**
     * 生产销售单位 (下拉框)
     */
    public String[][] findCompanyByCodes(String keyWord, Pageable page) throws QueryException {
        List<Object> objects = findObjectBySql(find(keyWord), page.getPageNumber(), page.getPageSize(), null);
        return reslut(objects);
    }


    /**
     * 生产销售单位 （下拉框） 2.0一线通
     */
    public String[][] findCompanyCode(String keyWord) throws QueryException {
        List<Object> objects = findObjectBySql(find(keyWord), 0, 0, null);
        return reslut(objects);
    }


    //生产销售单位  返回结果
    private String[][] reslut(List<Object> objects) {
        String[][] companyInfo = new String[objects.size()][7];
        for (int i = 0; i < objects.size(); i++) {
            Object[] o = (Object[]) objects.get(i);
            companyInfo[i][0] = String.valueOf(o[1]);// 单位名称
            companyInfo[i][1] = String.valueOf(o[0]);// 单位代码
            companyInfo[i][2] = String.valueOf(o[2]);// 单位地址
            companyInfo[i][3] = String.valueOf(o[3]);// 单位电话
            companyInfo[i][4] = String.valueOf(o[4]);// 单位传真
            companyInfo[i][5] = String.valueOf(o[5]);// 18位社会代码
            companyInfo[i][6] = String.valueOf(o[6]);// 信息状态，0审核中，1已审核
        }
        return companyInfo;
    }

    /**
     * 使用code查询供应商地址、电话、传真
     * @param code 编码
     */
    @RequestMapping("/companyQp/query_address_phone_fax")
    public String queryCompanyQpAddressAndPhoneAndFax(String code){
        String format = "{\"address\":\"%s\", \"phone\":\"%s\", \"fax\":\"%s\"}";
        List<Object[]> list = companyQpDao.queryCompanyQpAddressAndPhoneAndFax(code);
        if(CollectionUtils.isEmpty(list) || list.size() > 1){
            return String.format(format, "", "", "");
        }
        Object[] apf = list.get(0);
        String address = apf[0] == null ? "" : apf[0].toString();
        String phone = apf[1] == null ? "" : apf[1].toString();
        String fax = apf[2] == null ? "" : apf[2].toString();
        return String.format(format, address, phone, fax);
    }

}
